var ns, toast, UIActionProgress, UISearchBar;
var app = new Vue({
    el: '#app',
    data: {
        headerHeight:160,
        companyName: '', //公司名称
        postionName: "", //岗位
        userHeadImg: "", //用户头像
        userName: "", //用户名称
        bannerimg: '../image/header_bg.png',
        IsMain: 1,
        list: [],
        TypeID: 0,
        IsShow: false,
        form: {
            pageindex: 0,
            pagesize: 10,
        },
        showsearch: false,
        showTop: false,
        name: '',
        typeurl: '',
        moduleList: [],
        touchID: 0,
        SortOrder: 0,
        newCount: 0,//消息未读数量 
        imglist:[],     
        tongzhilist:[],//通知公告列表
        list:[],
    },
    methods: {
        get_data: function () {
            var that = this;
            that.get_user_data();//获取顶部个人信息
            that.get_newCount();//获取消息未读数量     
            that.headerHeight=document.getElementById('aui-header').offsetHeight+10;               
            that.get_gonggao();//获取通知公告
            that.get_list();//获取列表          
        },
        get_list:function(){
            var that = this;
            var options = {};
            options.action = "get_user_task_list";
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(succeed));
                // console.log(JSON.stringify(err));
                toast.hide();
                if (succeed) {
                   if(data.status){
                        that.list=data.resData;
                   }
                } 
            });  
        },
        get_newCount:function(){
            var that = this;
            var options = {};
            options.action = "getsysmenulist";
            options.IsMain=that.IsMain;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.newCount=data.NoReadCount;
                } 
            });
        },
        get_gonggao: function () {
            var that = this;
            var options = {};
            options.pageindex = that.form.pageindex;
            options.pagesize = that.form.pagesize;
            // options.IsReadonly =false;
            options.action = "getmsglist";
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    that.tongzhilist = data.tongzhilist;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        get_user_data: function () {
            var that = this;
            // 公司名称
            var companyName = api.getPrefs({
                sync: true,
                key: 'companyName'
            });
            // 岗位名称
            var positionname = api.getPrefs({
                sync: true,
                key: 'positionname'
            });
            // 用户名称
            var username = api.getPrefs({
                sync: true,
                key: 'username'
            });
            // 用户头像
            var headimg = api.getPrefs({
                sync: true,
                key: 'headimg'
            });
            that.companyName = companyName || '重庆永友网络科技有限公司';
            that.postionName = positionname ? positionname : "";
            that.userHeadImg = headimg || '../image/default.png';
            that.userName = username || '匿名用户';
        },
        do_open_scan_login: function (item) {
            var that = this;
            ns.confirmPer('camera', function () {
                setTimeout(function () {
                    that.do_open_scan_login_start();
                }, 200)
            })
        },
        do_open_scan_login_start: function () {
            var that = this;
            ns.openFrame('scanner_frm', {
                type: 0,
                IsHome:true,
            });
        },
        //点击遮罩关闭弹窗
        doHideSearch: function () {
            var that = this;
            that.showsearch = false;
            that.showTop = false;
            that.IsMain = 1;
            that.get_data();
        },
        do_opentypelist:function(ID){
            var title;
            if(ID==814){
                title="客服工单待办";
                ns.openWin('customerservice_renwulist',title,{
                    IsWorkOrder:1,
                    servicetype:1
                });
                return;
            }
            if(ID==695){
                title="投诉建议";
                ns.openWin('customerservice_renwulist',title,{
                    IsWorkOrder:2,
                    servicetype:2
                });
                return;
            }
            if(ID==690){
                var lineStatusDesc= api.getPrefs({
                    sync: true,
                    key: 'lineStatus_equipment'
                });
                if(lineStatusDesc==1){
                    title="设备作业待办(离线模式)";
                    ns.openWin('renwulist_equipment_offlist',title);
                    return;
                }
                else{
                    title="设备作业待办";
                    ns.openWin('equipment_renwulist',title);
                    return;
                } 
            }
            if(ID==692){
                var lineStatusDesc= api.getPrefs({
                    sync: true,
                    key: 'lineStatus'
                });
                if(lineStatusDesc==1){
                    title="安防巡更待办(离线模式)";
                    ns.openWin('renwulist_offlist',title);
                    return;
                }
                else{
                    title="安防巡更待办";
                    ns.openWin('patrolplan_renwulist',title);
                    return;
                } 
            }
            if(ID==693){
                title="保洁绿化待办";
                ns.openWin('bjlh_renwulist',title);
                return;
            }
            if(ID==963){
                title="通知公告";
                ns.openWin('tongzhigonggao_renwulist',title);
                return;
            }
            if(ID==956){
                title="审批待办";
                ns.openWin('examine_renwulist',title);
                return;
            }
            if(ID==1064){
                title="内部通知";
                ns.openWin('neibutz_renwulist',title);
                return;
            }
            if(ID==1564){
                title="工作任务";
                ns.openWin('worktasklist_frm',title);
                return;
            }
        },
        //打开个人中心
        openSetting: function () {
            ns.openWin('gerenzhongxin_frm', '个人信息');
        },
        //打开搜索
        do_open_sousuo: function () {
            ns.openWin('home_sousuo', '搜索中心');
        },
        //打开消息中心
        do_open_news: function () {
            ns.openWin('home_news_center', '消息中心');
        },
        open_tongzhixiaoxi: function () {
            // ns.openWin('tongzhixiaoxi_frm', '通知公告');
            ns.openWin('tongzhigonggao_renwulist','通知公告');
        },
        open_gonggao_detail: function (id, title) {
            var that = this;
            ns.openWin('tongzhixiaoxidetail_frm', '消息详情', {
                id: id
            });
            api.sendEvent({
                name:'home-refresh'
            })
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    UIActionProgress = api.require('UIActionProgress');
    toast = new auiToast();
    
    setTimeout(function () {
        app.get_data();   
        ns.init_pull();
    
    }, 300)   
    setTimeout(function () {
        appupgrade.check_upgrade(false);
    }, 5000)
    api.addEventListener({
        name: 'changecentersuccess'
    }, function () {
        app.get_user_data();
    });

    api.addEventListener({
        name: 'isreadtrue'
    }, function (ret, err) {
        if (ret) {
            setTimeout(function () {
                app.get_data();
            }, 500);
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });
        }
    });
    api.addEventListener({
        name: 'isreadCustomer'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadEquipment'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadPatrol'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadBjlh'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadTongzhi'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadExamine'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'home_new_updata'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'purchase_updata'
    }, function () {
        app.get_data();
    }); 
    api.addEventListener({
        name: 'home-refresh'
    }, function () {
        app.get_data();
    }); 
}
